// define a mixin object

const Tracking = {
  onLoad (opt) {
    this.$currentPage = {
      enterTime: 0,
      leaveTime: 0,
      title: '小书宝',
      desc: '',
      path: 'pages/index/index'
    }
    if (opt.bookId) {
      opt.bookId = Number(opt.bookId) || ''
    }
    // 页面是否是分享 1分享 0正常进入
    console.log('Tracking', opt)
    // this._getOptBookId(opt)
    // #ifdef MP-WEIXIN
    // this._updateIsShareedPageValue(opt.sharePage || 0)
    // this._getCurrentPagePath()
    // this.trackStudyPackage(opt)
    // #endif
  },
  // 导航之间切换
  onHide () {
    this._updateIsShareedPageValue(0)
  },
  // 页面之间返回
  onUnload () {
    this._updateIsShareedPageValue(0)
  },
  // 分享朋友事件
  onShareAppMessage (share) {
    uni.flux.track.ShareSuccess({
      property: {
        has_logged_in: uni.flux.store.state.userInfo.phone ? 1 : 0,
        page: this.$currentPage.path
      }
    })
    if (share && share.from === 'button') return // 判断分享来自的类型

    return {
      title: this.$currentPage.title,
      content: this.$currentPage.desc,
      path: this.$currentPage.path + '&sharePage=1',
      query: '',
      desc: this.$currentPage.desc,
      imageUrl: this.$currentPage.imageUrl,
      success: success => {
        console.log(success, '分享成功')
      },
      fail: (error) => {
        console.error(error, '分享失败')
      }
    }
  },
  methods: {
    /**
     * H5点击小程序 进入 小书宝首页及子页 埋点统计
     */
    trackStudyPackage (opt) {
      if (this.$store.state.env.isWeixin && opt) {
        // 精品  简版 h5点击允许跳转小程序小书宝埋点
        if (opt.isSampleVersion === '0') {
          uni.flux.track.boutiqueStudyPackage.BoutiqueHomePageShow({ book_id: `${opt.bookId}` })
          return
        }
        if (opt.isSampleVersion === '1') {
          uni.flux.track.conciseStudyPackage.SimpleHomePageShowAllow({ book_id: `${opt.bookId}` })
        }
      }
    },
    _getOptBookId (opt) {
      if (opt.bookId) {
        // 更新bookId
        this.$store.commit('setBookId', opt.bookId)
        // 更新书籍 场景
        const { fullPath } = uni.flux.utils.getCurrentPageFullPath()
        if (fullPath && fullPath.includes('/pages/book')) {
          this.$store.dispatch('updateBookScene', opt.bookId)
        }
      }
    },
    _getCurrentPagePath () {
      // 获取当前路由
      // eslint-disable-next-line no-undef
      const pages = getCurrentPages()
      const router = pages.length > 1 ? pages[pages.length - 1] : pages[0]
      const routerOptions = router.options || {}
      this.$currentPage.path = router.route + '?'

      let query = ''

      Object.keys(routerOptions).map((key, index) => {
        query += index === 0 ? `${key}=${routerOptions[key]}` : `&${key}=${routerOptions[key]}`
      })

      this.$currentPage.path = this.$currentPage.path + query

      // console.log('【query】: ', router.options)
    },
    _updateIsShareedPageValue (val) {
      // #ifdef MP-WEIXIN
      if (this.$store.state.isSharePage) {
        return this.$store.commit('setSharePage', 0)
      }
      if (val) {
        this.$store.commit('setSharePage', 1)
      }
      // #endif
    }
  }
}

export default Tracking
